import logging

from flask import Blueprint, request
from flask import jsonify

from airflow.patsnap.service import pool_service, user_service
from airflow.models import Pool
from airflow.www.api import exception_handle, provide_user

log = logging.getLogger(__name__)

api_speech_pool = Blueprint('api_speech_pool', __name__)


@api_speech_pool.route('/pools/<int:pid>', methods=['GET'])
@exception_handle
def get_pool(pid):
    pool = Pool.get_pool_id(pid)
    return jsonify(code=0, message='ok', data=Pool.to_json(pool))


@api_speech_pool.route('/pools', methods=['GET'])
@exception_handle
def get_pools():
    page = request.args.get('page', 0, type=int)
    result = pool_service.get_pools(page)
    return jsonify(code=0, message='ok', data=result)


@api_speech_pool.route('/pools', methods=['POST'])
@exception_handle
@provide_user
def add_pool(oa_user_name):
    slot_pool = request.json
    slots = slot_pool.get('slots', None)
    desc = slot_pool.get('desc', None)
    name = slot_pool.get('pool', None)
    is_admin = user_service.is_admin(oa_user_name)
    if not is_admin:
        return jsonify(code=500, message='不是管理员，无权限!', data=None)
    if name is None:
        log.error("add_pool error pool=%s", name, )
        return jsonify(code=400, message="add_pool error pool=None", data=None)
    if slots is None:
        log.error("add_pool error slots=%s", slots, )
        return jsonify(code=400, message="add_pool error slots=None", data=None)
    pool_service.add_pool(name, slots, desc)
    return jsonify(code=0, message='ok', data=None)


@api_speech_pool.route('/pools/<int:pid>', methods=['DELETE'])
@exception_handle
@provide_user
def delete_pool(pid, oa_user_name):
    is_admin = user_service.is_admin(oa_user_name)
    if not is_admin:
        return jsonify(code=500, message='不是管理员，无权限!', data=None)
    result = pool_service.delete_pool(pid)
    if result:
        return result
    return jsonify(code=0, message='ok', data=None)


@api_speech_pool.route('/pools/stats', methods=['GET'])
@exception_handle
def pool_stat():
    pools = Pool.slots_stats(lock_rows=False)
    return jsonify(code=0, message='ok', data=pools)


@api_speech_pool.route('/pools', methods=['PUT'])
@exception_handle
@provide_user
def update_pool(oa_user_name):
    slot_pool = request.json
    pid = slot_pool.get('id', None)
    slots = slot_pool.get('slots', None)
    desc = slot_pool.get('desc', None)
    name = slot_pool.get('pool', None)

    is_admin = user_service.is_admin(oa_user_name)
    if not is_admin:
        return jsonify(code=500, message='不是管理员，无权限!', data=None)
    if pid is None:
        log.error("update_pool error id=%s", pid, )
        return jsonify(code=400, message="add_pool error id=None", data=None)
    if name is None:
        log.error("add_pool error pool=%s", name, )
        return jsonify(code=400, message="add_pool error name=None", data=None)
    if slots is None:
        log.error("add_pool error slots=%s", slots, )
        return jsonify(code=400, message="add_pool error slots=None", data=None)
    result = pool_service.update_pool(pid, name, slots, desc)
    if result:
        return result
    return jsonify(code=0, message='ok', data=None)
